import pandas as pd
import numpy as np

# 读取数据
data = pd.read_csv('final/ratings.csv')

# 创建用户索引
user_idx = data['userId'].unique()
user_idx_map = {user_id: idx for idx, user_id in enumerate(user_idx)}

# 创建电影索引
movie_idx = data['movieId'].unique()
movie_idx_map = {movie_id: idx for idx, movie_id in enumerate(movie_idx)}

# 创建稀疏矩阵
rows = data['userId'].map(user_idx_map)
cols = data['movieId'].map(movie_idx_map)
values = data['rating']

# 创建评分矩阵
rating_matrix = np.zeros((len(user_idx), len(movie_idx)))
rating_matrix[rows, cols] = values

# 进行矩阵分解等操作...





# 使用 pandas DataFrame 包含索引信息
user_index = pd.Series(user_idx, name='userId')
movie_index = pd.Series(movie_idx, name='movieId')

rating_df = pd.DataFrame(rating_matrix, index=user_index, columns=movie_index)